<?php

class UserDao {

    /**
     * audit_aion字段说明
     * 1:添加用户审核
     * 2：更新用户审核
     * 3:删除用户审核
     * 4:用户输入错误密码超过规定限制
     */
    /**
     * 获取用户列表或指定用户
     * @param int $user_id	用户ID
     * @param bool $type  true查找管理员 false查找普通用户
     * return arraygetUserList
     */
    public static function getUserList($user_id='',$type=true,$where='1') {
        $db = DB_MySQLi::getInstance();
        $T_SQL="is_oper=0 and ";
        if($type){
            $T_SQL="is_oper=1 and ";
        }
        if (!empty($user_id)) {
            $sql = "select * from user_user where ".$T_SQL." id=" . $user_id;
        } else {
            $sql = "select * from user_user where ".$T_SQL.$where ." order by id";
        }
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }

    /**
     * 获取用户组列表或指定用户组信息
     * @param int $directory_id	用户目录组ID
     * return array
     */
    public static function getGroupList($directory_id) {
        $db = DB_MySQLi::getInstance();
        if (!empty($directory_id)) {
            $sql = "select * from user_directory where id=" . $directory_id ;
        } else {
            $sql = "select * from user_directory order by id";
        }
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }

    /**
     * 获取部门列表或指定部门信息
     * @param int $group_id	用户组ID
     * return array
     */
    public static function getDepartmentList($dep_id) {
        $db = DB_MySQLi::getInstance();
        if (!empty($dep_id)) {
                $sql = "select * from user_group where id=" . $dep_id ;
        } else {
            $sql = "select * from user_group order by id";
        }
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }


    /**
     * 添加部门
     * @param str $dep_name 部门名称
     */
    public static function sqlAddDep($dep_name,$username,$up_name) {
        $db = DB_MySQLi::getInstance();
        $name = $db->escapeString($dep_name);
        $sql = "insert into user_group(nickname,is_forbidden,up_id,create_time,create_name) values ('" . $name . "','1','".$up_name."',now(),'" . $username . "')";
        $query = $db->query($sql);
        if ($query) {
            return 1;
        }
        return 2;
    }
    /**
     * 编辑部门
     * @param str $dep_name 部门名称
     * @param str $dep_id 部门ID
     */
    public static function sqlUpDep($dep_name,$dep_id,$up_dep_name) {
        $db = DB_MySQLi::getInstance();
        $sql="update user_group set nickname='".$dep_name."',up_id='".$up_dep_name."' where id='" . intval($dep_id)."'" ;

        $query = $db->query($sql);
            if ($query) {
                return true;
            } else {
                return false;
            }
    }

    /**
     * 物理删除该部门
     * @param int $dep_id 部门ID
     */
    public static function sqlDelEnableDep($dep_id){
        $db = DB_MySQLi::getInstance();
        $sql="delete from user_group where id='" . intval($dep_id)."'";
        $query = $db->query($sql);
            if ($query) {
                return true;
            } else {
                return false;
            }
    }


    /**
     * 添加用户
     * @param array $userdata 用户信息
     * return bool
     */
    public static function sqlAddUser($userdata) {
        $db = DB_MySQLi::getInstance();
        $enphrase=substr(md5(time()),0,8);
        $time=time();
        if(!empty($user_data['addTime'])){
            $time=$user_data['addTime'];
        }
        $sql="insert into user_user(username,password,level,nickname,email,phone,card,is_forbidden,group_id,group_nickname,enphrase,is_oper,ip_adress,login_land,joined_time,pwd_time) values ('" . $userdata['username'] . "','" . $userdata['password']. "','".$userdata['level']."','" . $userdata['nickname'] . "','" . $userdata['email'] . "','" . $userdata['phone'] . "','".$userdata['iccard']."','1','".$userdata['groundid']."','".$userdata['groundname']."','".$enphrase."','0','".$userdata['ip_adress']."','".$userdata['login_land']."','".$time."','".time()."')";

        $query = $db->query($sql);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改用户信息
     * @param type $where sql条件
     * @param int $userid 用户ID
     * @param int $uptype 是否修改审核状态
     * @return int
     */
    public static function sqlUpUser($where,$userid,$uptype=''){
        $db = DB_MySQLi::getInstance();
        $sql="update user_user set ".$where." where id='" . intval($userid)."'" ;
        $query = $db->query($sql);
            if ($query) {
                if(!empty($uptype)){
                    self::sqlUpForbidden($userid,$uptype);
                    $db->query("update user_user set audit_aion='2' where id='" . intval($userid)."'" );
                }
                return 1;
            } else {
                return 2;
            }
    }

    /**
     * 添加用户 | 更新用户 | 删除用户,暂时将信息添加至审核表中
     */
    public static function sqlPendingUser($name,$userJSON,$audit_aion,$userid=''){
        $db = DB_MySQLi::getInstance();
        $username = $db->escapeString($name['username']);
        $query = $db->query("insert into user_user_bak(userid,username,nickname,json_data,audit_aion) values ('".$userid."','".$username."','".base64_encode($name['nickname'])."','".$userJSON."','".$audit_aion."')");
        if ($query) {
            return true;
        }
        return false;
    }

    /**
     * 2013查找待审核用户
     */
    public static function sqlAuditUser($audit_id='',$where = ''){
        $db = DB_MySQLi::getInstance();
        if (!empty($audit_id)) {
            $sql = "select * from user_user_bak where id=" . $audit_id;
        } else {
            $sql = "select * from user_user_bak where ".$where ." order by id";
        }
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }

    /**
     * 2014查找被锁管理员
     */
    public static function sqlAdminUser($where = ''){
        $db = DB_MySQLi::getInstance();
        $sql = "select * from user_user where ".$where ." and is_oper='1' and is_forbidden='1' order by id";
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }
    /**
     * 修改管理员IP地址
     * @param type $userid
     * @param type $ip
     * @return boolean
     */
    public static function sqlUpAdminIpAdress($userid,$ip){
        $db = DB_MySQLi::getInstance();
        $query=$db->query("update user_user set ip_adress= '".$ip."' where id = '" . intval($userid)."'");
            if ($query) {
                return true;
            } else {
                return false;
            }
    }

    /**
     * 修改审计状态
     * @param int $type 1:审计通过 2:未通过审计
     */
    public static function sqlUpForbidden($userid,$type){
        $db = DB_MySQLi::getInstance();
        $query=$db->query("update user_user set is_forbidden= '".$type."' where id = '" . intval($userid)."'");
            if ($query) {
                return true;
            } else {
                return false;
            }
    }
    /**
     * 物理删除该用户(彻底删除该用户)
     * @param int $userid 用户ID
     */
    public static function sqlDelEnableUser($userid){
        $db = DB_MySQLi::getInstance();
        $sql=$db->query("delete from user_user where id='" . intval($userid)."'");
        if ($sql) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据用户名查找该用户信息
     * @param type $username
     */
    public static function sqlUserData($username){
        $db = DB_MySQLi::getInstance();
         if (!empty($username)) {
            $sql = "select * from user_user where username='".$username."'";
         }
         $query = $db->getData_ASSOCS($sql);
         return $query;
    }

    /**
     * 审核成功修改备份表中用户信息
     */
//    public static function sqlUpUserBak($userid,$data){
//        $db = DB_MySQLi::getInstance();
//        $sql="update user_user_bak set json_data ='".$data."' where userid='" . intval($userid)."'" ;
//        $query = $db->query($sql);
//            if ($query) {
//                return true;
//            } else {
//                return false;
//            }
//    }

    /**
     * 删除审核表中的信息
     * @param type $userid
     */
    public static function sqlDelUserBak($audit_id){
        $db = DB_MySQLi::getInstance();
        $sql=$db->query("delete from user_user_bak where id='" . intval($audit_id)."'");
        if ($sql) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 备份表记录日志ID
     * @param type $username
     * @param type $logid
     * @return boolean
     */
    public static function sqlUpUserBakLog($username,$logid){
        $db = DB_MySQLi::getInstance();
        $sql="update user_user_bak set log_id= '".$logid."' where username = '" .$username."'";
        $query=$db->query($sql);
            if ($query) {
                return true;
            } else {
                return false;
            }
    }

    /**
     * 根据权限不同获取该权限下所有管理员账号
     * @param type $directory_id
     * @return type
     */
    public static function sqlAdminList($directoryId){
        $db = DB_MySQLi::getInstance();
        $sql = "select * from user_user where directory_id=".$directoryId ." order by id";
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }

    /**
     * 添加管理员
     */
    public static function sqlAddAdmin($userdata){
        $db = DB_MySQLi::getInstance();
        $sql="insert into user_user(username,password,directory_id,is_forbidden,is_oper,joined_time,pwd_time) values ('" . $userdata['username'] . "','" . $userdata['password']. "','".$userdata['directoryId']."','0','1','".time()."','".time()."')";
        $query = $db->query($sql);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 锁定恢复用户
     */
    public static function lock_recovery($type,$userid){
        $db = DB_MySQLi::getInstance();
        $sql="update user_user set is_forbidden= '".$type."' where id = '" .$userid."'";
        $query=$db->query($sql);
            if ($query) {
                return true;
            }
        return false;
    }

    /**
     * 查找当前用户密码复杂度的设置
     */
    public static function sql_getComplexity(){
        $db = DB_MySQLi::getInstance();
        $sql = "select * from user_pwd ";
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }

    /**
     * 修改密码复杂度设置
     */
    public static function sql_upComplexity($pwdType,$pwdLength,$id){
        $db = DB_MySQLi::getInstance();
        $sql="update user_pwd set pwdType={$pwdType},pwdLength={$pwdLength} where id ={$id}";
        $query=$db->query($sql);
        return $query;
    }

    /**
     * 查找具备审核权限的用户
     */
    public static function sql_getExamine(){
        $db = DB_MySQLi::getInstance();
        $sql = "select id,username,group_id,group_nickname from user_user where is_admin=1";
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }

    /**
     * 根据用户ID获取用户名
     */
    public static function sql_getUserName($userid){
        $db = DB_MySQLi::getInstance();
        $sql = "select username,group_id from user_user where id=".$userid;
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }
    
    /**
     * 查找所有用户名
     */
    public static function sql_Name(){
        $db = DB_MySQLi::getInstance();
        $sql = "select * from user_user";
        $query = $db->getData_ASSOCS($sql);
        return $query;
    }
}
?>
